import defaultValue from './defaultValue'
import store from '@/store'
import { typeudjfn } from './defaultValue'

let utypstr = defaultValue.utype.map((e) => {
  return e.value
})
const searchjson = [
  { label: '用户名', value: 'uUser' },
  { label: '联系人', value: 'uxm' },
  { label: '联系电话', value: 'utel' },
  {
    label: '用户类型',
    value: 'sutype',
    type: 'select',
    defaultValue: `utype in (${utypstr.toString()})`,
    option: [
      { label: '全部', value: `utype in (${utypstr.toString()})` },
      ...defaultValue.utype.map((e) => {
        return { label: e.label, value: `utype in (${e.value})` }
      }),
    ],
  },
  {
    async callbackasync() {
      let data = await store.dispatch('citystation/getstation')
      let r = data.filter((e) => {
        return e.type == '1'
      })
      return {
        label: '所属站点',
        value: 'pcs',
        type: 'select',
        option: [
          { label: '全部', value: `` },
          ...r.map((e) => {
            return { label: e.pcs, value: e.id }
          }),
        ],
        defaultValue: '',
      }
    },
  },
]
// label: '', value: 'pcs', type: 'pcs'
// getstation
const tablejson = [
  { label: '用户名', value: 'uUser' },
  { label: '联系人', value: 'uxm' },
  { label: '联系电话', value: 'utel' },
  {
    label: '用户类型',
    value: 'utype',
    type: 'select',

    option: [...defaultValue.utype],
  },
  {
    async tablecallbackasync() {
      let data = await store.dispatch('citystation/getstation')
      let r = data.filter((e) => {
        return e.type == '1'
      })
      return {
        label: '所属站点',
        value: 'pcs',
        type: 'select',
        option: [
          ...r.map((e) => {
            return { label: e.pcs, value: e.id }
          }),
        ],
        defaultValue: '',
        rule: [
          {
            required: true,
            message: '请输入',
            trigger: ['blur', 'change'],
          },
        ],
      }
    },
  },
]
if (store.state.user.token.utype == 3) {
  tablejson.splice(1, 0, { label: '密码', value: 'dma' })
}

let Uedtjson = [
  { value: 'uUser', label: '用户名', type: 'button' },
  {
    value: 'utype',
    label: '用户类型',
    type: 'select',
    option: [...defaultValue.utype],
    defaultValue: defaultValue.utype[0].value,
  },

  {
    async callbackasync() {
      let data = await store.dispatch('citystation/getstation')
      let r = data.filter((e) => {
        return e.type == '1'
      })
      return {
        label: '所属站点',
        value: 'pcs',
        type: 'select',
        option: [
          ...r.map((e) => {
            return { label: e.pcs, value: e.id }
          }),
        ],
        defaultValue: '',
        rule: [
          {
            required: true,
            message: '请输入',
            trigger: ['blur', 'change'],
          },
        ],
      }
    },
  },
  {
    callbackasync: async (data) => {
      if (
        !['0', '11', '12', '14', '18'].find((e) => {
          return e == data.utype
        })
      ) {
        return []
      }

      if (!data.pcs) {
        return []
      }

      // let
      // if(data.frid){

      // }
      let StatFr = await store.dispatch('statfr/getstatfrone', {
        pcs: data.pcs,
      })
      let option = [{ value: '0', label: '不设置' }]
      let typeutypedz = {
        0: ['0'],
        11: ['2', '3', '4'],
        12: ['5'],
        14: ['0'],
        18: ['0'],
      }
      StatFr.forEach((e) => {
        if (
          e.type != 1 &&
          e.frid != store.state.user.token.frid &&
          typeutypedz[data.utype] &&
          typeutypedz[data.utype].find((f) => {
            return f == e.type
          })
        ) {
          option.push({ label: e.mc ? e.mc : '', value: e.frid })
        }
      })
      if (
        data.frid &&
        !option.find((e) => {
          return e.value == data.frid
        })
      ) {
        StatFr = await store.dispatch('statfr/getstatfrone', {
          id: data.frid,
        })
        if (StatFr.length == 1) {
          let e = StatFr[0]
          option.push({ label: e.mc ? e.mc : '', value: e.frid })
        }
      }
      let r = [
        {
          value: 'frid',
          label: '分润',
          type: 'select',
          option: option,
          if: { zhi: [{ value: 'utype', zhi: ['0', '11', '12', '14', '18'] }] },
          // defaultValue: '0',
        },
      ]
      // await new Promise((resolve) => {
      //   setTimeout(() => {
      //     resolve(151)
      //   }, 500)
      // })
      return r
    },
  },
  {
    callback: (data) => {
      let r = [
        {
          value: 'udj',
          label: '用户等级',
          type: 'select',
          option: 'udj',
          defaultValue: '0',
          if: {
            zhi: [{ value: 'utype', zhi: ['11', '12', '0', '7', '15'] }],
          },
        },
      ]
      r[0].option = typeudjfn(data)
      //

      return r
    },
  },
  {
    value: 'dl',
    label: '发卡限额',
    fl: '100',
    defaultValue: '0',
    if: {
      zhi: [{ value: 'utype', zhi: ['21'] }],
    },
  },
  {
    value: 'uxm',
    label: '联系人',
  },
  {
    value: 'utel',
    label: '电话号码',
  },
  {
    callback: (data) => {
      let r = []
      let bzstr = '备注'
      if (
        ['11', '20'].find((e) => {
          return e == data.utype
        })
      ) {
        bzstr = '物业名称'
      }
      r.push({
        value: 'bz',
        label: bzstr,
        if: {
          zhi: [
            {
              value: 'utype',
              zhi: [
                '0',
                '1',
                '2',
                '3',
                '4',
                '5',
                '6',
                '7',
                '8',
                '9',
                '10',
                '12',
                '13',
                '14',
                '15',
                '16',
                '17',
                '18',
                '19',
              ],
            },
          ],
        },
      })
      return r
    },
  },
]

let Uaddjson = [
  {
    value: 'uUser',
    label: '用户名',
    rule: [
      {
        required: true,
        message: '请输入用户名',
        trigger: 'blur',
      },
    ],
  },
  {
    callback: (data) => {
      console.log('callbackcallback', data)
      let r = []
      r.push({
        value: 'dma',
        label: '密码',
        type: 'password',
        rule: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
          {
            message: '密码最少为6位',
            trigger: 'blur',
            validator(rule, value, callback) {
              if (value.length < 6) {
                callback(new Error('error'))
              } else {
                callback()
              }
            },
          },
        ],
      })
      function qrdma(value, callback) {
        if (data.dma != value) {
          callback(new Error('error'))
        } else {
          callback()
        }
      }
      r.push({
        value: 'qrdma',
        label: '确认密码',
        type: 'password',
        rule: [
          {
            required: true,
            message: '请再次输入密码',
            trigger: 'change',
            validator(rule, value, callback) {
              qrdma(value, callback)
            },
          },
        ],
      })

      return r
    },
  },

  {
    value: 'utype',
    label: '用户类型',
    type: 'select',
    option: [...defaultValue.utype],
    defaultValue: defaultValue.utype[0].value,
  },
  {
    async callbackasync() {
      let data = await store.dispatch('citystation/getstation')
      let r = data.filter((e) => {
        return e.type == '1'
      })
      return {
        label: '所属站点',
        value: 'pcs',
        type: 'select',
        option: [
          ...r.map((e) => {
            return { label: e.pcs, value: e.id }
          }),
        ],
        defaultValue: '',
        rule: [
          {
            required: true,
            message: '请输入',
            trigger: ['blur', 'change'],
          },
        ],
      }
    },
  },
  {
    callbackasync: async (data) => {
      if (
        !['0', '11', '12', '14', '18'].find((e) => {
          return e == data.utype
        })
      ) {
        return []
      }

      if (!data.pcs) {
        return []
      }
      let StatFr = await store.dispatch('statfr/getstatfrone', {
        pcs: data.pcs,
      })
      let option = [{ value: '0', label: '不设置' }]
      let typeutypedz = {
        0: ['0'],
        11: ['2', '3', '4'],
        12: ['5'],
        14: ['0'],
        18: ['0'],
      }
      StatFr.forEach((e) => {
        if (
          e.type != 1 &&
          e.frid != store.state.user.token.frid &&
          typeutypedz[data.utype] &&
          typeutypedz[data.utype].find((f) => {
            return f == e.type
          })
        ) {
          option.push({ label: e.mc ? e.mc : '', value: e.frid })
        }
      })
      let r = [
        {
          value: 'frid',
          label: '分润',
          type: 'select',
          option: option,
          if: { zhi: [{ value: 'utype', zhi: ['0', '11', '12', '14', '18'] }] },
          defaultValue: '0',
        },
      ]
      return r
    },
  },
  {
    callback: (data) => {
      let r = [
        {
          value: 'udj',
          label: '用户等级',
          type: 'select',
          option: 'udj',
          defaultValue: '0',
          if: {
            zhi: [{ value: 'utype', zhi: ['11', '12', '0', '7', '15'] }],
          },
        },
      ]

      r[0].option = typeudjfn(data)
      //

      return r
    },
  },
  {
    value: 'dl',
    label: '发卡限额',
    fl: '100',
    defaultValue: '0',
    if: {
      zhi: [{ value: 'utype', zhi: ['21'] }],
    },
  },
  {
    value: 'uxm',
    label: '联系人',
  },
  {
    value: 'utel',
    label: '电话号码',
  },
  {
    callback: (data) => {
      let r = []
      let bzstr = '备注'
      if (
        ['11', '20'].find((e) => {
          return e == data.utype
        })
      ) {
        bzstr = '物业名称'
      }
      r.push({
        value: 'bz',
        label: bzstr,
        if: {
          zhi: [
            {
              value: 'utype',
              zhi: [
                '0',
                '1',
                '2',
                '3',
                '4',
                '5',
                '6',
                '7',
                '8',
                '9',
                '10',
                '12',
                '13',
                '14',
                '15',
                '16',
                '17',
                '18',
                '19',
              ],
            },
          ],
        },
      })
      return r
    },
  },
]
let Udmaedt = [
  { label: '用户名', value: 'uUser', type: 'button' },
  {
    label: '旧密码',
    value: 'olddma',
    type: 'password',
    rule: [
      {
        required: true,
        message: '请输入旧密码',
        trigger: 'change',
      },
    ],
  },
  {
    label: '新密码',
    value: 'dma',
    type: 'password',
    rule: [
      {
        required: true,
        message: '请输入新密码',
        trigger: 'change',
      },
    ],
  },
  {
    callback(data) {
      return [
        {
          label: '确认密码',
          value: 'dmaqr',
          type: 'password',
          rule: [
            {
              required: true,
              message: '请再次输入密码',
              trigger: 'change',
              validator(rule, value, callback) {
                if (data.dma != value) {
                  callback(new Error('error'))
                } else {
                  callback()
                }
              },
            },
          ],
        },
      ]
    },
  },
]
// 参数名	类型	说明
// uUser	输入	用户名
// olddma	输入	旧密码
// dma	输入	新密码
// par	输出	参考附录表par 返回信息
let defaultdatavalue = { pcs: store.getters['user/token'].pcs }
export default {
  defaultdatavalue,
  searchjson,
  tablejson,
  utypestr: `utype in (${utypstr.toString()})`,
  Uedtjson,
  Uaddjson,
  Udmaedt,
}
